package com.example.tree.controller;

import com.example.common.domain.PageResult;
import com.example.common.domain.Result;
import com.example.tree.domain.dto.TreeDTO;
import com.example.tree.domain.dto.TreePageDTO;
import com.example.tree.domain.vo.MarkersVO;
import com.example.tree.domain.vo.TreeDetailVO;
import com.example.tree.domain.vo.TreeListVO;
import com.example.tree.domain.vo.TreeStatisticsVO;
import com.example.tree.service.TreeService;
import com.google.zxing.WriterException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Tag(name = "古树接口相关")
@Slf4j
@RestController
@RequestMapping("/tree")
@RequiredArgsConstructor
public class AncientTreeController {
    private final TreeService treeService;


    /**
     * 获取古树列表
     * @param treePageDTO
     * @return
     */
    @GetMapping
    @Operation(summary = "获取古树列表")
    public Result<PageResult<TreeListVO>> getTreeInfo(TreePageDTO treePageDTO ){
        log.info("获取古树列表:{}", treePageDTO);
        PageResult<TreeListVO> pageResult =  treeService.getTreeInfo(treePageDTO);
        return Result.success(pageResult);
    }


    /**
     * 获取古树详情信息
     * @param treeId
     * @return
     */
    @GetMapping("/details/{treeId}")
    @Operation(summary = "获取古树详细信息")
    public Result<TreeDetailVO> getDetailTreeInfo(@PathVariable Integer treeId) {
        return Result.success(treeService.getDetailTreeInfo(treeId));
    }



    @Transactional
    @DeleteMapping
    @Operation(description = "批量删除古树信息")
    public Result delete(@RequestBody Integer[] ids) {
        log.info("批量删除古树信息:{}", Arrays.toString(ids));
        //  批量删除古树信息
        treeService.delete(ids);
        return Result.success();
    }

    @GetMapping("/markers")
    @Operation(summary = "获取区域古树位置信息")
    public Result<List<MarkersVO>> getTreeMarkers(String level,String code) {
        log.info("获取区域古树位置信息:{}", level + ":" + code);
        List<MarkersVO> treeMarkers = treeService.getTreeMarkers(level, code);
        return Result.success(treeMarkers);
    }

    @GetMapping("/statistics")
    @Operation(summary = "获取区域古树统计信息")
    public Result<TreeStatisticsVO> getTreeStatistics(String level, String code) {
        TreeStatisticsVO treeStatistics  = treeService.getTreeStatistics(level, code);
        return Result.success(treeStatistics);
    }

    /**
     * 添加古树信息
     * @param treeDTO
     * @return
     */
    @PostMapping
    @Operation(summary = "添加古树信息")
    public Result addTreeInfo(@RequestBody @Validated TreeDTO treeDTO) throws IOException, WriterException {
        log.info("添加古树信息:{}", treeDTO);
        treeService.addTreeInfo(treeDTO);
        return Result.success();
    }

}
